JOP Reference Handbook

نویسنده

  • Martin Schoeberl
چکیده

This is the main entry point for the scheduler. This method has to be overridden to implement the scheduling algorithm. It is called from the JVM on a timed event or a software interrupt (see genInt()) is issued (e.g. when a Task gives up execution). vo i d i n t e r r u p t ( i n t nr ) The scheduler is notified on a hardware event. It can directly call an associated device driver or use this information to unblock a waiting task. vo i d mo n i t o r E n t e r ( Object o ) vo i d mo n i t o r E x i t ( Object o ) These methods are invoked by the JVM on synchronized methods and blocks (JVM bytecodes monitorenter and monitorexit). They provide hooks for executing dynamic priority changes in the scheduler. Scheduler Services of the JVM needed to implement a scheduler are provided through static methods. s t a t i c f i n a l v o i d g e n I n t ( ) This service from the JVM schedules a software interrupt. As a result, schedule() is called. This method is the standard way of switching control to the scheduler. It is e.g. invoked by block(). s t a t i c f i n a l v o i d e n a b l e I n t ( ) s t a t i c f i n a l v o i d d i s a b l e I n t ( ) 6.2 USER-DEFINED SCHEDULER 133 The scheduler cannot use monitors to protect its data structures as the scheduler itself is in charge of handling monitors. To protect the data structures of the scheduler, it can globally enable and disable interrupts. s t a t i c f i n a l v o i d d i s p a t c h ( Task nextTask , i n t nextTim ) This method dispatches a Task and schedules a timer interrupt at nextTim. s t a t i c f i n a l v o i d a t t a c hD a t a ( Object obj , Ob ject data ) s t a t i c f i n a l Object g e tA t t a c h e dDa t a ( Object ob j ) The behavior of the priority inversion avoidance protocol is defined by the user scheduler. The root of the Java class hierarchy (java.lang.Object) contains a JVM internal reference of generic type Object that can be used by the scheduler to attach data structures for monitors. The first argument of these methods is the object that is used as monitor. Scheduler or Task The following two methods are utility functions useful for the scheduler and the thread implementation. s t a t i c f i n a l i n t getNow ( ) To support time-triggered scheduling, the system provides access to a high-resolution time or counter. The returned value is the time since startup in microseconds. The exact resolution is implementation-dependent. s t a t i c f i n a l Task g e tRu n n i n gTa s k ( ) The current running Task (in which context the scheduler is called) is returned by this method. 6.2.5 Class Task A basic structure for schedulable objects is shown in Listing 6.4. This class is usually extended to provide a thread implementation that fits to the user-defined scheduler. The class Task is intended to be minimal. To avoid inheriting methods that do not fit for some applications, it does not extend java.lang.Thread. However, Task can be used to implement java.lang.Thread. The methods enterMemory and exitMemory are used by the application to provide scoped memory for temporary allocated objects. Task provides a list of active tasks for the scheduler. One issue, raised by the implementation of the framework is the way in which access rights to methods need to be defined in Java. All methods, except start(), should be private or protected. However, some methods, such as schedule(), are invoked by a part of the JVM, which is also written in Java but resides in a different package. This results in defining the methods as public and hoping that they are not invoked by the application code. The C++ concept of friends would greatly help in sharing information over package boundaries without making this information public. 134 6 JOP RUNTIME SYSTEM p u b l i c c l a s s Task { p u b l i c Task ( ) p u b l i c Task (Memory mem) vo i d s t a r t ( ) p u b l i c vo i d en te rMemory ( ) p u b l i c vo i d e x i tMemo r y ( ) p u b l i c vo i d r u n ( ) s t a t i c Task g e t F i r s t T a s k ( ) s t a t i c Task g e tNe x tTa s k ( ) } Listing 6.4: A basic schedulable object 6.2.6 A Simple Example Scheduler Listing 6.5 shows a full example of using this framework to implement a simple round robin scheduler. The only method that needs to be supplied is schedule(). For a more advanced scheduler, it is necessary to provide a combination of a user defined thread class and a scheduler class. These two classes have to be tightly integrated, as the scheduler uses information provided by the thread objects for its scheduling decisions. 6.2 USER-DEFINED SCHEDULER 135 p u b l i c c l a s s RoundRobin ex t end s Schedu l e r { // // t e s t t h r e ad s // s t a t i c c l a s s Work ex t end s Task { p r i v a t e i n t c ; Work ( i n t ch ) { c = ch ; } p u b l i c vo i d run ( ) { f o r ( ; ; ) { Dbg . wr ( c ) ; // debug output // busy wa i t to s imu l a t e // 3 ms work load i n Work . i n t t s = Schedu l e r . getNow ( ) ; t s += 3000; wh i l e ( t s−Schedu l e r . getNow()>0) ; } } } // // u s e r s c h e d u l e r s t a r t s he r e // p u b l i c vo i d addTask ( Task t ) { // we do not a l l ow t a s k s to be // added a f t e r s t a r t ( ) . } // // c a l l e d by the JVM // p u b l i c vo i d s c h edu l e ( ) { Task t = getRunningTask ( ) . getNextTask ( ) ; i f ( t==n u l l ) t = Task . g e t F i r s tT a s k ( ) ; 136 6 JOP RUNTIME SYSTEM d i s p a t c h ( t , getNow ()+10000) ; } p u b l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) { new Work( ’ a ’ ) ; new Work( ’ b ’ ) ; new Work( ’ c ’ ) ; RoundRobin r r = new RoundRobin ( ) ; r r . s t a r t ( ) ; } } Listing 6.5: A very simple scheduler 6.2.7 Interaction of Task, Scheduler and the JVM The framework is used to re-implement the scheduler described in Section 6.1. In the original implementation, the interaction between scheduling and threads was simple, as the scheduling was part of the thread class. Using the framework, these functions have to be split to two classes, extending Task and Scheduler. Both classes are placed in the same package to provide simpler information sharing with some protection from the rest of the application. For performance reasons data structures are directly exposed from one class to the other. The resulting implementation is compatible with the first definition, with the exception that RtThread now extends Task. However, no changes in the application code are necessary. Figure 6.1 is an interaction example of this scheduler within the framework. The interaction diagram shows the message sequences between two application tasks, the scheduler, the JVM and the hardware. The hardware represents interrupt and timer logic. The corresponding code fragments of the application, RtThread and PriorityScheduler are shown in Listing 6.6, 6.7 and 6.8. Task 2 is a periodic task with a higher priority than Task 1. The first event is a timer event to unblock Task 2 for a new period. The generated timer event results in a call of the user defined scheduler. The scheduler performs its scheduling decision and issues a context switch to Task 2. With every context switch the timer is reprogrammed to generate an interrupt at the next time triggered event for a higher priority task. Task 2 performs the periodic work and ceases execution by invocation of waitForNextPeriod(). The scheduler is called and requests an interrupt from the hardware resulting in the same call sequence as with a timer or other hardware 6.2 USER-DEFINED SCHEDULER 137 Task 1 Scheduler Task 2 JVM Hardware

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Handbook of Research on Digital Libraries: Design, Development, and Impact

The Handbook of Research on Digital Libraries: Design, Development, and Impact is the single academic reference work to provide comprehensive coverage of digital libraries. Through predictions of future trends, examinations of techniques and technologies, and focuses on users, interactions, and experiences, this in-depth collection provides developers and scholars with an extensive collection o...

متن کامل

Designing and producing a reference book with LATEX: The Engineer’s Quick Reference Handbook

This article describes the process of designing an Italian reference book, namely Il prontuario dell’ingegnere. As a reference work it shares the characteristics of any other such book; it also uses a large amount of mathematics and contains a large number of graphics insertions. Moreover, the publisher wanted to distribute the work both as a regular bound book and as a computer file to be read...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2007